﻿using System;
using System.Collections.Generic;

namespace Genesis.Domain
{

    using Genesis.Infrastructure;
    using Genesis.Infrastructure.Domain;

    public class Reply : Entity
    {

        private IList<Reply> replies = new List<Reply>();

        [Signature]
        public virtual User User { get; protected set; }

        [Signature]
        public virtual DateTime PostedOn { get; protected set; }

        [Signature]
        public virtual string HostAddress { get; protected set; }

        public virtual string ParsedMessage { get; protected set; }

        public virtual string RawMessage { get; protected set; }

        public virtual bool IsApproved { get; set; }

        public virtual IEnumerable<Reply> Replies { get { return replies; } }

        public virtual void AddReply(Reply reply)
        {
            
            Logic.Check(reply != null, "Reply can not be null!");
            Logic.Check(reply != this, "Reply can not be self!");
            
            if (!replies.Contains(reply))
                replies.Add(reply);
        
        }

        public virtual void RemoveReply(Reply reply)
        {
        
            Logic.Check(reply != null, "Reply can not be null!");
            Logic.Check(reply != this, "Reply can not be self!");
            
            if (replies.Contains(reply))
                replies.Remove(reply);
        
        }

        public virtual void SetParsedMessage(string parsedMessage)
        {
        
            Logic.Check(!string.IsNullOrEmpty(parsedMessage) && !string.IsNullOrWhiteSpace(parsedMessage), "ParsedMessage can not be null, empty or whitespace!");
            
            ParsedMessage = parsedMessage.Trim();
        
        }

        public virtual void SetRawMessage(string rawMessage)
        {
        
            Logic.Check(!string.IsNullOrEmpty(rawMessage) && !string.IsNullOrWhiteSpace(rawMessage), "RawMessage can not be null, empty or whitespace!");
            
            RawMessage = rawMessage.Trim();
        
        }

        public Reply(User user, DateTime postedOn, string hostAddress)
        {
        
            Logic.Check(user != null, "User can not be null!");
            Logic.Check(postedOn.CompareTo(DateTime.Now) <= 0, "PostedOn can not be in the future!");
            Logic.Check(!string.IsNullOrEmpty(hostAddress) && !string.IsNullOrWhiteSpace(hostAddress), "HostAddress can not be null, empty or whitespace!");
            
            User = user;
            PostedOn = postedOn;
            HostAddress = hostAddress.Trim();
        
        }

        protected Reply() { }

    }

}